﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class REFMOVData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";

        public int ThreadCount;
        private string OrderNum;
        DataTable tbWGSummary;
        public REFMOVData()
            : base()
        {
            BuildTrisList();
            tbWGSummary = new DataTable("WGFSUM");

            tbWGSummary.Columns.Add("FACTORY", typeof(string));
            tbWGSummary.Columns.Add("ARTICLE", typeof(string));
            tbWGSummary.Columns.Add("WBEWBESCHICH", typeof(string));
            tbWGSummary.Columns.Add("COLORDES", typeof(string));
            tbWGSummary.Columns.Add("WBBDPART", typeof(string));
            tbWGSummary.Columns.Add("WBEWMTR", typeof(double));
            tbWGSummary.Columns.Add("PCNUM", typeof(int));
        }
        public REFMOVData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;32;27;21;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                /*
                 SELECT /* +FIRST_ROWS
 WBEWWERTDAT AS WBEWBUCHDAT,lpad(substr(WBEWBUCHUHR,1,length(WBEWBUCHUHR)-2),2,'0') as WBEWBUCHUHR,F.CUSTOMER AS FACTORY,
'RETURN' AS TYPE,WBEWBESCHICH,
TRIM(WBEWART)||'-'||TRIM(WBEWFARBE)||'-'||TRIM(WBEWVAR) AS ARTICLE,WBEWBS,WBBDPART,
WBEWFC,SUM(CASE WHEN WBEWMTR <0 THEN -1 ELSE 1 END ) AS PCNUM,
ROUND(SUM(WBEWMTR)*0.001/0.9144,2) AS WBEWMTR
FROM WBBEW W,WBBST B,WBDAT C,
(SELECT TO_CHAR(TO_NUMBER(D.VVLIEFSCH)) AS DN,TRIM(E.KDNAME1) AS CUSTOMER
 FROM VVLIEF D,KDSTAMM E WHERE D.VVMANDANT = E.KDMANDANT
 AND D.VVKUNDNRE = E.KDKUNDNR) F
WHERE W.WBEWMANDANT = B.WBBMANDANT AND W.WBEWRWKRS = B.WBBRWKRS
AND W.WBEWRWLFD = B.WBBRWLFD AND W.WBEWPOS = B.WBBPOS
AND W.WBEWMANDANT = C.WBMANDANT AND W.WBEWRWKRS = C.WBRWKRS
AND W.WBEWRWLFD = C.WBRWLFD AND TRIM(C.WBLIEFSCHNR) = F.DN(+)
AND WBEWWAKZ='F' AND W.WBEWMTR <> 0 AND W.WBEWLP ='TH      '
AND WBEWBS IN ('10','11','01')
GROUP BY WBEWART,WBEWFARBE,WBEWVAR,WBEWBS,WBBDPART,WBEWBESCHICH,WBEWFC,
WBEWWERTDAT,lpad(substr(WBEWBUCHUHR,1,length(WBEWBUCHUHR)-2),2,'0'),CUSTOMER
UNION ALL
SELECT /* +FIRST_ROWS
 WBEWBUCHDAT,lpad(substr(WBEWBUCHUHR,1,length(WBEWBUCHUHR)-2),2,'0') as WBEWBUCHUHR,trim(KDNAME1) AS FACTORY,
'RETURN' AS TYPE,WBEWBESCHICH,
TRIM(WBEWART)||'-'||TRIM(WBEWFARBE)||'-'||TRIM(WBEWVAR) AS ARTICLE,WBEWBS,WBBDPART,
WBEWFC,SUM(CASE WHEN WBEWMTR <0 THEN -1 ELSE 1 END ) AS PCNUM,
ROUND(SUM(WBEWMTR)*0.001/0.9144,2) AS WBEWMTR
FROM WBBEW W,WBBST B,VKPOS E,KDSTAMM F
WHERE W.WBEWMANDANT = B.WBBMANDANT(+) AND W.WBEWRWKRS = B.WBBRWKRS(+)
AND W.WBEWRWLFD = B.WBBRWLFD(+) AND W.WBEWPOS = B.WBBPOS(+)
AND W.WBEWMANDANT = E.VKPMANDANT AND W.WBEWAUFKRS = E.VKPAUFKRS
AND W.WBEWAUFLFD = E.VKPAUFLFD AND W.WBEWAUFPOS = E.VKPAUFPOS
AND E.VKPSA <>'AJ' AND E.VKPMANDANT = F.KDMANDANT
AND E.VKPKUNDNR = F.KDKUNDNR AND WBEWWAKZ='F'
AND WBEWBS = '32' AND WBEWFC = 'VV06    '
GROUP BY WBEWART,WBEWFARBE,WBEWVAR,WBEWBS,WBBDPART,WBEWBESCHICH,WBEWFC,
WBEWBUCHDAT,lpad(substr(WBEWBUCHUHR,1,length(WBEWBUCHUHR)-2),2,'0'),KDNAME1;
                 */
                or1.CommandText = "SELECT /* +FIRST_ROWS*/ "
                    + " WBEWWERTDAT AS WBEWBUCHDAT,lpad(substr(WBEWBUCHUHR,1,length(WBEWBUCHUHR)-2),2,'0') as WBEWBUCHUHR,F.CUSTOMER AS FACTORY, "
                    + " 'RETURN' AS TYPE,WBEWBESCHICH, "
                    + " TRIM(WBEWART)||'-'||TRIM(WBEWFARBE)||'-'||TRIM(WBEWVAR) AS ARTICLE,WBEWBS,WBBDPART, "
                    + " WBEWFC,SUM(CASE WHEN WBEWMTR <0 THEN -1 ELSE 1 END ) AS PCNUM, "
                    + " ROUND(SUM(WBEWMTR)*0.001/0.9144,2) AS WBEWMTR ,'' AS COLORDES "
                    + " FROM WBBEW W,WBBST B,WBDAT C, "
                    + "(SELECT TO_CHAR(TO_NUMBER(D.VVLIEFSCH)) AS DN,TRIM(E.KDNAME1) AS CUSTOMER "
                    + "  FROM VVLIEF D,KDSTAMM E WHERE D.VVMANDANT = E.KDMANDANT "
                    + "  AND D.VVKUNDNRE = E.KDKUNDNR) F "
                    + " WHERE W.WBEWMANDANT = B.WBBMANDANT AND W.WBEWRWKRS = B.WBBRWKRS "
                    + " AND W.WBEWRWLFD = B.WBBRWLFD AND W.WBEWPOS = B.WBBPOS "
                    + " AND W.WBEWMANDANT = C.WBMANDANT AND W.WBEWRWKRS = C.WBRWKRS "
                    + " AND W.WBEWRWLFD = C.WBRWLFD AND TRIM(C.WBLIEFSCHNR) = F.DN(+) "
                    + " AND WBEWWAKZ='F' AND W.WBEWMTR <> 0 AND W.WBEWLP ='TH      ' "
                    + " AND WBEWBS IN ('10','11','01') "
                    + " AND WBEWWERTDAT>='" + WBSRTDATE + "' AND WBEWWERTDAT<='" + WBENTDATE + "'"
                    + " GROUP BY WBEWART,WBEWFARBE,WBEWVAR,WBEWBS,WBBDPART,WBEWBESCHICH,WBEWFC, "
                    + " WBEWWERTDAT,lpad(substr(WBEWBUCHUHR,1,length(WBEWBUCHUHR)-2),2,'0'),CUSTOMER "
                    + " order by 1,2,3 ";
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "WBBEW";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", WBBDPART, FACTORY, WBEWBESCHICH,COLORDES;
                    int PCNUM;
                    double WBEWMTR;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListWGSummary = new List<WGSummary>();
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    { 
                        Column = "FACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "ARTICLE";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "WBEWBESCHICH";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "WBBDPART";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        setLocal = GetCOLORDES(setLocal, dwTemp);
                        if (FilterData(dwTemp, ref dltRows))
                        {
                            FACTORY = dwTemp["FACTORY"].ToString().Trim();
                            ARTICLE = dwTemp["ARTICLE"].ToString().Trim();
                            WBEWBESCHICH = dwTemp["WBEWBESCHICH"].ToString().Trim();
                            COLORDES = dwTemp["COLORDES"].ToString().Trim(); 
                            WBBDPART = dwTemp["WBBDPART"].ToString().Trim();
                            PCNUM = System.Convert.ToInt16(dwTemp["PCNUM"].ToString());
                            WBEWMTR = System.Convert.ToDouble(dwTemp["WBEWMTR"].ToString());
                            var query = from WGSummary wgsum in ListWGSummary
                                        where
                                       wgsum.FACTORY == FACTORY
                                            &&wgsum.ARTICLE == ARTICLE
                                        && wgsum.WBBDPART == WBBDPART
                                        && wgsum.WBEWBESCHICH == WBEWBESCHICH
                                        select wgsum;
                            WGSummary wgSum = null;
                            foreach (WGSummary wgsum in query)
                            {
                                wgSum = wgsum;
                                wgSum.PCNUM += PCNUM;
                                wgsum.WBEWMTR += WBEWMTR;
                            }
                            if (wgSum == null)
                            {
                                wgSum = new WGSummary(FACTORY,ARTICLE,WBEWBESCHICH,COLORDES, WBBDPART, PCNUM, WBEWMTR);
                                ListWGSummary.Add(wgSum);
                            }
                        }
                    }
                    foreach (DataRow dwTemp in dltRows)
                    {
                        dwTemp.Delete();
                    }
                    setLocal.AcceptChanges();
                    DataRow dwWGFSUM = null;
                    tbWGSummary.Clear();
                    foreach (WGSummary wgsum in ListWGSummary)
                    {
                        dwWGFSUM = tbWGSummary.NewRow(); 
                        dwWGFSUM["FACTORY"] = wgsum.FACTORY;
                        dwWGFSUM["ARTICLE"] = wgsum.ARTICLE;
                        dwWGFSUM["WBEWBESCHICH"] = wgsum.WBEWBESCHICH;
                        dwWGFSUM["COLORDES"] = wgsum.COLORDES;
                        dwWGFSUM["WBBDPART"] = wgsum.WBBDPART;
                        dwWGFSUM["WBEWMTR"] = wgsum.WBEWMTR;
                        dwWGFSUM["PCNUM"] = wgsum.PCNUM;
                        tbWGSummary.Rows.Add(dwWGFSUM);
                    }
                    set4Return.Merge(tbWGSummary);

                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet GetCOLORDES(DataSet dsTarget, DataRow dwABPOS)
        {
            string TableName = "COLORDES";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT DISTINCT SUBSTR(SDDATEN,2,30) AS COLORDES FROM SDDAT "
                    + " WHERE SUBSTR(SDKEY,1,2)='00' AND trim(SUBSTR(SDKEY,3,10))=:ABPBESCHICH  AND SDSA = '9998' "
                     ;
                orlReadCommand.Parameters.Add("ABPBESCHICH", OracleType.NVarChar).Value = dwABPOS["WBEWBESCHICH"].ToString().Trim();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;

                    byte[] b;
                    string value;
                    string ColumnName = "";
                    foreach (DataRow dw in dsResult.Tables["COLORDES"].Rows)
                    {
                        ColumnName = "COLORDES";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;

                        dwABPOS["COLORDES"] = dw["COLORDES"];
                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        private bool FilterData(DataRow dwWBBEW, ref List<DataRow> dltRows)
        {
            try
            {
                string WBEWBS = dwWBBEW["WBEWBS"].ToString().Trim();
                string WBEWFC = dwWBBEW["WBEWFC"].ToString().Trim();

                if (!TrisInclude.Contains(WBEWBS + ";"))
                {
                    if (WBEWBS == "13"
                        && WBEWFC == "AV10")
                    {
                        return true;
                    }
                    if (WBEWBS == "32"
                        && WBEWFC == "VV06")
                    {
                        return true;
                    }
                    dltRows.Add(dwWBBEW);
                    return false;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return true;
        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}